package util;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginFilter implements Filter {
   
	private String logonURL;

   public void init(FilterConfig fc) {
      logonURL = fc.getInitParameter("Index.jsp");
   }

   public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
      HttpServletRequest request = (HttpServletRequest) req;
      HttpServletResponse response = (HttpServletResponse) res;

      HttpSession session = request.getSession();
      Boolean loginError = (Boolean) session.getAttribute("LoginError");

      if (loginError == null || loginError == true) {
         String requestURL = request.getRequestURL().toString();
         session.setAttribute("forwardURL",requestURL);
         RequestDispatcher dispatcher = request.getRequestDispatcher(logonURL);
         if (dispatcher != null) dispatcher.forward(request,response);
      } else {
         chain.doFilter(request,response);
      }
   }

   public void destroy() {
   }
}
